package com.ea.wxwork.service;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ea.util.StringUtil;
import com.ea.wxwork.api.vo.WxworkDeptVO;
import com.ea.wxwork.api.vo.WxworkUserBaseInfoVO;
import com.ea.wxwork.config.WxworkConfig;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName WxworkIntegrateService
 * @Description TODO
 * @Author XIONG CAI
 * @Date 2024/11/12
 */
@Service
public class WxworkIntegrateService {


    public String getAccessToken(){
        String wxworkApiDomain = WxworkConfig.getWxworkApiDomain();
        String corpId = WxworkConfig.getCorpId();
        String corpSecret = WxworkConfig.getCorpSecret();
        String accessTokenUrl = wxworkApiDomain + "/cgi-bin/gettoken?corpid=" + corpId + "&corpsecret=" + corpSecret;

        String result = HttpUtil.get(accessTokenUrl);
        if (StringUtil.isNotEmpty(result)) {
            JSONObject jsonObject = JSONObject.parseObject(result);
            if (jsonObject.getString("errcode").equals("0")) {
                String accessToken = jsonObject.getString("access_token");
                return accessToken;
            }else {
                return jsonObject.getString("errmsg");
            }
        }else {
            return "获取accessToken失败";
        }
    }

    /**
     * 获取所有部门列表
     * @param accessToken
     * @return
     */
    public List<WxworkDeptVO>  getAllDeptList(){
        String accessToken = this.getAccessToken();
        if (StringUtil.isEmpty(accessToken)) {
            return null;
        }
        String wxworkApiDomain = WxworkConfig.getWxworkApiDomain();
        String deptListUrl = wxworkApiDomain + "/cgi-bin/department/list?access_token=" + accessToken;
        String result = HttpUtil.get(deptListUrl);
        if (StringUtil.isNotEmpty(result)) {
            JSONObject jsonObject = JSONObject.parseObject(result);
            if (jsonObject.getString("errcode").equals("0")) {
                JSONArray department = jsonObject.getJSONArray("department");
                List<WxworkDeptVO> deptList = new ArrayList<>();
                for (int i = 0; i < department.size(); i++) {
                    JSONObject deptJson = department.getJSONObject(i);
                    WxworkDeptVO wxworkDeptVO = new WxworkDeptVO();
                    wxworkDeptVO.setDeptId(deptJson.getString("id"));
                    wxworkDeptVO.setName(deptJson.getString("name"));
                    wxworkDeptVO.setParentId(deptJson.getString("parentid"));
                    wxworkDeptVO.setOrder(deptJson.getInteger("order"));
                    wxworkDeptVO.setNameEn(deptJson.getString("department_leader"));
                    deptList.add(wxworkDeptVO);
                }
                return deptList;
            }else {
                return null;
            }
        }else {
            return null;
        }
    }

    /**
     *
     * @param accessToken
     * @return
     */
    public List<WxworkUserBaseInfoVO>  getAllUserList(){
        String accessToken = this.getAccessToken();
        List<WxworkDeptVO> allDeptList = getAllDeptList();

        for (WxworkDeptVO wxworkDeptVO : allDeptList) {

        }


        String wxworkApiDomain = WxworkConfig.getWxworkApiDomain();
        String userListUrl = wxworkApiDomain + "/cgi-bin/user/list?access_token=" + accessToken;
        String result = HttpUtil.get(userListUrl);
        if (StringUtil.isNotEmpty(result)) {
            JSONObject jsonObject = JSONObject.parseObject(result);
            if (jsonObject.getString("errcode").equals("0")) {
                JSONArray userList = jsonObject.getJSONArray("userlist");
                List<WxworkUserBaseInfoVO> userBaseInfoList = new ArrayList<>();
                for (int i = 0; i < userList.size(); i++) {
                    JSONObject userJson = userList.getJSONObject(i);
                    WxworkUserBaseInfoVO wxworkUserBaseInfoVO = new WxworkUserBaseInfoVO();
                    wxworkUserBaseInfoVO.setUserId(userJson.getString("userid"));
                    wxworkUserBaseInfoVO.setName(userJson.getString("name"));
                    //wxworkUserBaseInfoVO.setDepartment(userJson.getString("department"));
                    wxworkUserBaseInfoVO.setMobile(userJson.getString("mobile"));
                    wxworkUserBaseInfoVO.setEmail(userJson.getString("email"));
                    wxworkUserBaseInfoVO.setAvatar(userJson.getString("avatar"));
                    userBaseInfoList.add(wxworkUserBaseInfoVO);
                }
                return userBaseInfoList;
            }else {
                return null;
            }
        }else {
            return null;
        }
    }
}
